package com.newjiabo.ski.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.newjiabo.common.core.enums.ResultCode;
import com.newjiabo.common.exception.ServiceException;
import com.newjiabo.common.utils.DateUtils;
import com.newjiabo.common.utils.SnowFlake;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.ski.domain.SkiOrder;
import com.newjiabo.ski.domain.SkiOrderDetail;
import com.newjiabo.ski.domain.SkiOrderDetailServices;
import com.newjiabo.ski.domain.enums.OrderStatusEnum;
import com.newjiabo.ski.domain.vo.OrderDetailInfoVo;
import com.newjiabo.ski.service.ISkiOrderDetailService;
import com.newjiabo.ski.service.ISkiOrderDetailServicesService;
import com.newjiabo.ski.task.AutomaticReceiptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.newjiabo.ski.mapper.SkiOrderMapper;
import com.newjiabo.ski.service.ISkiOrderService;

import javax.annotation.Resource;

/**
 * 订单Service业务层处理
 *
 * @author CaoMengHui
 * @date 2023-09-22
 */
@Service
public class SkiOrderServiceImpl implements ISkiOrderService {
    @Resource
    private SkiOrderMapper skiOrderMapper;
    @Resource
    private ISkiOrderDetailService orderDetailService;
    @Resource
    private ISkiOrderDetailServicesService orderDetailServicesService;
    @Resource
    private AutomaticReceiptService automaticReceiptService;
    private static final Logger log = LoggerFactory.getLogger(SkiOrderServiceImpl.class);

    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public SkiOrder selectSkiOrderByOrderId(String orderId) {
        return skiOrderMapper.selectSkiOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param skiOrder 订单
     * @return 订单
     */
    @Override
    public List<SkiOrder> selectSkiOrderList(SkiOrder skiOrder) {
        return skiOrderMapper.selectSkiOrderList(skiOrder);
    }

    /**
     * 新增订单
     *
     * @param skiOrder 订单
     * @return 结果
     */
    @Override
    public int insertSkiOrder(SkiOrder skiOrder) {
        if(StringUtils.isEmpty(skiOrder.getOrderId())){
            skiOrder.setOrderId(SnowFlake.getIdStr());
        }
        skiOrder.setDelFlag(false);
        skiOrder.setCreateTime(DateUtils.getNowDate());
        return skiOrderMapper.insertSkiOrder(skiOrder);
    }

    /**
     * 修改订单
     *
     * @param skiOrder 订单
     * @return 结果
     */
    @Override
    public int updateSkiOrder(SkiOrder skiOrder) {
        skiOrder.setUpdateTime(DateUtils.getNowDate());
        return skiOrderMapper.updateSkiOrder(skiOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteSkiOrderByOrderIds(Long[] orderIds) {
        return skiOrderMapper.deleteSkiOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteSkiOrderByOrderId(Long orderId) {
        return skiOrderMapper.deleteSkiOrderByOrderId(orderId);
    }

    @Override
    public Boolean orderStatusHandle(String outTradeNo, String transactionId, OrderStatusEnum orderStatusEnum, Date successTime, double payTotal) {
        SkiOrder skiOrder = this.selectByOutTradeNo(outTradeNo);
        if (ObjectUtil.isNull(skiOrder)) {
            throw new ServiceException(ResultCode.UNABLE_TO_FIND_INFORMATION_FOR_ORDER);
        }
        if (!OrderStatusEnum.canceled.name().equals(skiOrder.getOrderStatus())) {
            log.info("重复回调！");
            return false;
        }
        // 发送购买订单信息延迟任务
        automaticReceiptService.produce(skiOrder.getOrderId());
        this.updateStatusAndPayNoByOrderNo(outTradeNo, transactionId, OrderStatusEnum.paid.name(),successTime,payTotal);
        return true;
    }

    @Override
    public OrderDetailInfoVo getInfoDetail(String orderId) {
        SkiOrder skiOrder = this.selectSkiOrderByOrderId(orderId);
        SkiOrderDetail orderDetail = orderDetailService.selectSkiOrderDetailByOrderId(orderId);
        List<SkiOrderDetailServices> orderDetailServices = orderDetailServicesService.selectByOrderDetailId(orderDetail.getId());
        return new OrderDetailInfoVo(skiOrder,orderDetail,orderDetailServices);
    }

    @Override
    public int updateStatusByOrderId(String orderId, OrderStatusEnum orderStatusEnum) {
        return skiOrderMapper.updateStatusByOrderId(orderId,orderStatusEnum.name());
    }

    @Override
    public int completeOrder(String orderId) {
        automaticReceiptService.completeOrder(orderId);
        return skiOrderMapper.updateStatusByOrderId(orderId,OrderStatusEnum.completed.name());
    }

    private int updateStatusAndPayNoByOrderNo(String outTradeNo, String transactionId, String orderStatus, Date successTime, double payTotal) {
        return skiOrderMapper.updateStatusAndPayNoByOrderNo(outTradeNo, transactionId, orderStatus,successTime,payTotal);
    }

    private SkiOrder selectByOutTradeNo(String outTradeNo) {
        SkiOrder skiOrder = skiOrderMapper.selectByOutTradeNo(outTradeNo);
        return skiOrder;
    }
}
